﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace DictPPC
{
    public partial class frmDict : Form
    {
        private List<DictionaryInfo> _dicInfoList;
        private Dictionary _dictionary;
        private int _dicIndex;

        private string _directory = (System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName).Replace(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].Name, "");

        public frmDict()
        {
            InitializeComponent();
            _dictionary = new Dictionary();
        }

        private void frmDict_Load(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load(_directory + "DictionaryInfo.xml");

            var records = from r in doc.Descendants("record")
                          select r;
            _dicInfoList = new List<DictionaryInfo>();
            
            foreach (XElement record in records)
            {
                DictionaryInfo dicInfo = new DictionaryInfo();
                dicInfo.Name = record.Element("name").Value;
                dicInfo.Prefix = record.Element("prefix").Value;
                _dicInfoList.Add(dicInfo);
            }

            for (int i = 0; i < _dicInfoList.Count; i++)
            {
                MenuItem mni = new MenuItem();
                mni.Text = _dicInfoList[i].Name;
                mni.Click += new EventHandler(this.mniLoaiTuDien_Click);     
                mniTuDien.MenuItems.Add(mni);
            }
            mniTuDien.MenuItems[0].Checked = true;
            _dicIndex = 0;

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void mniThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mniLoaiTuDien_Click(object sender, EventArgs e)
        {
            ((MenuItem)sender).Checked = true;
            for(int i = 0;i < mniTuDien.MenuItems.Count;i ++)
            {
                if (mniTuDien.MenuItems[i].Text != ((MenuItem)sender).Text)
                {
                    mniTuDien.MenuItems[i].Checked = false;
                }
                else
                {
                    _dicIndex = i;
                }
            }
        }

        private void picTra_Click(object sender, EventArgs e)
        {
            tbxKetQuaTra.Text = "";
            if (cmbTuKhoa.Text != "")
            {
                string uri = _directory + "Data\\" + _dicInfoList[_dicIndex].Prefix
                    + "\\" + _dicInfoList[_dicIndex].Prefix;

                if (char.IsDigit(cmbTuKhoa.Text[0]))
                {
                    uri += "_0";
                }
                else
                {
                    uri += "_" + cmbTuKhoa.Text[0];
                }

                string result = _dictionary.Search(cmbTuKhoa.Text, uri);
                if (result == "")
                {
                    tbxKetQuaTra.Text = "Không có từ này trong từ điển";
                }
                else
                {
                    tbxKetQuaTra.Text = result;
                }
            }
        }

        private void cmbTuKhoa_TextChanged(object sender, EventArgs e)
        {
            //if (_numKeyChars == 0 && cmbTuKhoa.Text.Length == 1)
            //{
                
            //}
            //_numKeyChars = cmbTuKhoa.Text.Length;
        }

        private void picNoi_Click(object sender, EventArgs e)
        {
            string soundName = cmbTuKhoa.Text + ".mp3";

            Sound s = new Sound(_directory + soundName);
            s.Playmp3();
        }


    }
}